package com.systop.commer.alipay.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.systop.busafter.task.service.TaskService;
import com.systop.commer.ResponseResult;
import com.systop.commer.alipay.entity.RefundRequestParams;
import com.systop.commer.alipay.entity.qrcode.PreOrderParams;
import com.systop.commer.alipay.entity.qrcode.QrServiceEntity;
import com.systop.commer.alipay.entity.qrcode.RefundQueryParams;
import com.systop.commer.alipay.service.AlipayService;
import com.systop.commer.pojo.Task;
import com.systop.commer.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/")
public class IndexController {

    @Autowired
    private AlipayService alipayService;
    @Autowired
    private TaskService taskService;

    @Resource
    private RedisUtil redisUtil;


    private static int ExpireTime = 60;
    /**
     * 退款
     * @param refundRequestParams
     * @return
     */
    @RequestMapping(value = "refund")
    public String refund(RefundRequestParams refundRequestParams) {
        System.out.println(refundRequestParams);
        return JSON.toJSONString(alipayService.pcRefund(refundRequestParams));
    }
    /**
     * 退款
     * @param refundRequestParams
     * @return
     */
    @RequestMapping(value = "refund1")
    public @ResponseBody
    ResponseResult refund1 (RefundRequestParams refundRequestParams) {
        System.out.println(refundRequestParams);
        return ResponseResult.build(200, "退款",alipayService.pcRefund(refundRequestParams));
    }
    /**
     * 退款查询
     * @param refundQueryParams
     * @return
     */
    @RequestMapping(value = "refund_query")
    public String refundQuery(RefundQueryParams refundQueryParams) {

        return JSON.toJSONString(alipayService.refundQuery(refundQueryParams));
    }

    /**
     * 二维码生成
     * @param model
     * @return
     */
    @RequestMapping(value = "qrcode")
    public String qrcode(AlipayTradePrecreateModel model) {
        return JSON.toJSONString(alipayService.qrcodePay(model));
    }


    /**
     * 支付成功回调页面1111111111111
     * @return
     */
    @RequestMapping(value = "success")
    public String successPage() {
        return "http://localhost/success.html";
    }



    /**
     * 支付成功回调页面2222222222222
     * @return
     */
    @RequestMapping(value = "user/myself")
    public String successPage1() {
        return "/user/myself";
    }

    /**
     * 支付回调
     * @param request
     * @return
     */
    @RequestMapping(value = "notify_url",method = RequestMethod.POST)
    public String notifyurl(HttpServletRequest request) {
        QrServiceEntity qrServiceEntity = alipayService.alipayNotify(request);
        int ii= Integer.parseInt((String) redisUtil.get("taskid"));
        Task task = taskService.TaskId(ii);
        task.setTask_deposit_state(1);
        taskService.updatatask(task);
        if (qrServiceEntity.isIsreceive()) {
            System.out.println("--------------------------成功---------------------------"+alipayService.alipayNotify(request));
            return "success";
        } else {
            System.out.println("-------------------------失败----------------------------"+alipayService.alipayNotify(request));
            return "fail";
        }
    }

    /**
     * 跳转到支付宝官网支付
     * @param preOrderParams
     * @return
     */
    @RequestMapping(value = "pcpay",method = RequestMethod.GET)
    public String pcPay(PreOrderParams preOrderParams) {
        System.out.println("---------++++++++++++--+++++++++++++++-------"+preOrderParams.getSubject()+"------+++++++++++++---+++++++++++++++++++++---------");
        System.out.println("---------++++++++++++--+++++++++++++++-------"+alipayService.pcPreOrder(preOrderParams)+"------+++++++++++++---++++++++++++---------");
        String key = "taskid" ;
       redisUtil.set(key,preOrderParams.getSubject(),ExpireTime);
        System.out.println(redisUtil.get("taskid"));

        return alipayService.pcPreOrder(preOrderParams);
    }

    /**
     * 跳转到支付宝官网支付
     * @param preOrderParams
     * @return
     */
    @RequestMapping(value = "pcpay1",method = RequestMethod.GET)
    public @ResponseBody
    ResponseResult pcPay1(PreOrderParams preOrderParams) {
      //  return alipayService.pcPreOrder(preOrderParams);
        return ResponseResult.build(200, "成功",alipayService.pcPreOrder(preOrderParams));
    }
}
